//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using OpenLS.Spreadsheet.Internals;

namespace OpenLS.Spreadsheet
{
	partial class Worksheet
	{
	    private  RootGroup _rootRowGroup;
        internal RootGroup RootRowGroup
        {
            get
            {
                if (_rootRowGroup == null)
                    _rootRowGroup = new RowRootGroup(this);
                return _rootRowGroup;
            }
        }
        internal RootGroup RootColumnGroup
        {
            get
            {
                if (_rootColumnGroup == null)
                    _rootColumnGroup = new ColumnRootGroup(this);
                return _rootColumnGroup;
            }
        }
        public ObservableCollection<ChildGroup> RowGroups
        {
            get
            {
                return RootRowGroup.Children;
            }
        }

	    private  RootGroup _rootColumnGroup;
        public ObservableCollection<ChildGroup> ColumnGroups
        {
            get
            {
                return RootColumnGroup.Children;
            }
        }

        public void GroupRows(Row firstRow, Row nextRow)
        {
            CheckArguments(firstRow, nextRow, "firstRow", "nextRow");
             GroupDimensions(RootRowGroup, firstRow, nextRow);
        }

        private void GroupDimensions<T>(RootGroup root, T first, T next) where T : Dimension
	    {
            using (Workbook.CreateUndoScope(StringTable.Group))
	        {
	            CheckArguments(first, next, "first", "next");
	            Group firstContainer = root.GetContainer(first);
	            Group lastContainer = root.GetContainer(next);
	            if (firstContainer == lastContainer && firstContainer == firstContainer.Root && false)
	            {
	                var group = ChildGroup.Create<T>(firstContainer, first, next);
	                group.IsExpanded = true;
	                firstContainer.AddChild(group);
	                return;
	            }
	            Group commonAncestor = Group.GetCommonAncestor(firstContainer, lastContainer);
                commonAncestor.GroupDimensions<Dimension>(first, next);
	        }
	    }
        

	    public void GroupColumns(Column firstColumn, Column nextColumn)
	    {
	        Debug.WriteLine("grouping " + firstColumn + " " + nextColumn); 
             GroupDimensions(RootColumnGroup, firstColumn, nextColumn);
        }

        private void CheckArguments(Dimension firstRow, Dimension nextRow, string firstName, string lastName)
        {
            if (firstRow == null)
                throw new ArgumentNullException(firstName);
            if (firstRow.Worksheet != this)
                throw new ArgumentException(firstName);
            if (nextRow == null)
                throw new ArgumentNullException(lastName);
            if (nextRow.Worksheet != this)
                throw new ArgumentException(lastName);
            if (firstRow.Index > nextRow.Index)
                throw new ArgumentException();
        }

	    public void UngroupRows(Row firstRow, Row nextRow)
	    {
            CheckArguments(firstRow, nextRow, "firstRow", "nextRow");
            Ungroup<Row>(RootRowGroup, firstRow, nextRow);
	        
	    }

        private void Ungroup<T>(RootGroup root, T first, T next) where T : Dimension
	    {
            using (Workbook.CreateUndoScope(StringTable.Ungroup))
            {
                Group firstContainer = root.GetContainer(first);
                Group lastContainer = root.GetContainer(next);
                Group commonAncestor = Group.GetCommonAncestor(firstContainer, lastContainer);
                commonAncestor.Ungroup(first, next);
            }
	    }

	    public void UngroupColumns(Column firstColumn, Column nextColumn)
	    {
            CheckArguments(firstColumn, nextColumn, "firstColumn", "nextColumn");
            Ungroup<Column>(RootColumnGroup, firstColumn, nextColumn);
        }
	}
}
